package genes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;





public class GeneSequence {

	public String geneSequence;
	private List<Codon> codonSequence;
	private List<Codon> aaSequence;

	public GeneSequence(String sequence) throws IOException{
		this.geneSequence=sequence;
		this.codonSequence=breakIntoCodons(sequence);
		this.aaSequence=setAminoAcids(this.getCodonSequence());
	}
	
	private List<Codon> setAminoAcids(List<Codon> codonSequence) {
		AminoAcids dic= new AminoAcids();
		List<Codon> cSeq= codonSequence;
		List<Codon> aaS= new ArrayList<Codon>();
		Iterator<Codon> iter= codonSequence.iterator();
		Codon temp = null;
		while(iter.hasNext()){
		temp= iter.next();
		
			//System.out.println(temp.getCodon());
			aaS.add(new Codon(dic.getAminoAcid(temp)));
		}
		return aaS;
	}



	public List<Codon> getCodonSequence() {
		return codonSequence;
	}
	
	private List<Codon> breakIntoCodons(String s) throws IOException{
	    List<Codon> result = new ArrayList<Codon>();
	    int i;
	    for (i = 0; i < s.length() - 2; i += 3) {
	        result.add(new Codon(s.charAt(i), s.charAt(i + 1), s.charAt(i + 2)));
	    }
	    // now how do you want to handle a string of a length such that
	    // s.length() % 3 != 0
	    // ?
	    return result;
	}
	
	public final List<Integer> getPosOfMutations(int posy){
		
		List<Integer> positions= new ArrayList<Integer>();
		positions.add(posy*3);
		positions.add(posy*3+1);
		positions.add(posy*3+2);
		return positions;
	}
	
	public void clear(){
		this.geneSequence=null;
	}

	private int compairPositions(Codon s1c, Codon s2c,int pos) {
		int result=0;
		char fst1= s1c.getFirstPos().charAt(0);
		char sec1= s1c.getSecondPos().charAt(0);
		char thrd1= s1c.getThirdPos().charAt(0);
		char fst2= s2c.getFirstPos().charAt(0);
		char sec2= s2c.getSecondPos().charAt(0);
		char thrd2= s2c.getThirdPos().charAt(0);
		
		if(!(fst1==fst2)){
			result= (((pos+1)*3-1)-2);
		}else{
			if(!(sec1==sec2)){
				result= (((pos+1)*3-1)-1);			
			}else{
				if(!(thrd1==thrd2)){
					result=(((pos+1)*3-1)-0);
				}
			}
		}
		return result;
			
		
	}

	public String printAminoAcids(){
		Iterator<Codon> iter = this.codonSequence.iterator();		
		String result ="[";
		while(iter.hasNext()){
		result= result + iter.next().codesFor()+" "; 
	}
		result= result+"]";
		return result;
}
	
	public String printSequence(){
		 List<Codon> result = new ArrayList<Codon>();
		    int i;
		    for (i = 0; i < this.geneSequence.length() - 2; i += 3) {
		        result.add(new Codon(this.geneSequence.charAt(i), 
		        		this.geneSequence.charAt(i + 1), 
		        		this.geneSequence.charAt(i + 2)));
		    }
		    String print ="[";
		    Iterator<Codon> iter = result.iterator();
			while(iter.hasNext()){
			print= print + iter.next().getCodon()+" "; 
		}
			print= print+"]";	
			return print;
	}

	public void printList(List<Integer> posOfMutations) {
		Iterator<Integer> iter = posOfMutations.iterator();
		while(iter.hasNext()){
			System.out.println(iter.next());
		}
		
	}

	public List<Codon> getAaSequence() {
			return aaSequence;
	}

	public void setAaSequence(List<Codon> aaSequence) {
		this.aaSequence = aaSequence;
	}
}


